import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: resolve => require(['../Layout/index.vue'], resolve),
    redirect: '/dashboard',
    meta: {
      title: '首页',
      isfixed: true,
      icon: 'el-icon-s-home'
    },
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: resolve => require(['../views/MainPage/MainPage.vue'], resolve),
        meta: {
          title: '首页',
          isfixed: true,
          icon: 'el-icon-s-home'
        }
      }
    ]
  },
  {
    path: '/article',
    name: 'article',
    component: resolve => require(['../Layout/index.vue'], resolve),
    redirect: '/articleType',
    meta: {
      title: '文章管理',
      isfixed: false,
      icon: 'el-icon-document'
    },
    children: [
      {
        path: '/articleType',
        name: 'articleType',
        component: resolve => require(['../views/ArticleManage/ArticleType.vue'], resolve),
        meta: {
          title: '文章类别',
          isfixed: false,
          icon: 'el-icon-menu'
        }
      },
      {
        path: '/articleList',
        name: 'articleList',
        component: resolve => require(['../views/ArticleManage/ArticleList.vue'], resolve),
        meta: {
          title: '文章列表',
          isfixed: false,
          icon: 'el-icon-s-order'
        }
      },
      {
        path: '/articlePublish',
        name: 'articlePublish',
        component: resolve => require(['../views/ArticleManage/ArticlePublish.vue'], resolve),
        meta: {
          title: '发表文章',
          isfixed: false,
          icon: 'el-icon-s-promotion'
        }
      },
      {
        path: '/articleUpdate',
        name: 'articleUpdate',
        component: resolve => require(['../views/ArticleManage/ArticleUpdate.vue'], resolve),
        meta: {
          isHidden: true,
          title: '修改文章',
          isfixed: false,
          icon: 'el-icon-s-promotion'
        }
      }
    ]
  },
  {
    path: '/personCenter',
    name: 'personCenter',
    component: resolve => require(['../Layout/index.vue'], resolve),
    redirect: '/basicInfo',
    meta: {
      title: '个人中心',
      isfixed: false,
      icon: 'el-icon-user-solid'
    },
    children: [
      {
        path: '/basicInfo',
        name: 'basicInfo',
        component: resolve => require(['../views/PersonCenter/BasicInfo.vue'], resolve),
        meta: {
          title: '基本资料',
          isfixed: false,
          icon: 'el-icon-folder-opened'
        }
      },
      {
        path: '/resetPassword',
        name: 'resetPassword',
        component: resolve => require(['../views/PersonCenter/ResetPassword.vue'], resolve),
        meta: {
          title: '重置密码',
          isfixed: false,
          icon: 'el-icon-refresh'
        }
      },
      {
        path: '/updateAvatar',
        name: 'updateAvatar',
        component: resolve => require(['../views/PersonCenter/UpdateAvatar.vue'], resolve),
        meta: {
          title: '更换头像',
          isfixed: false,
          icon: 'el-icon-picture-outline'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: resolve => require(['../views/Login/Login.vue'], resolve),
    meta: {
      isfixed: false,
      isHidden: true,
      title: '用户登录'
    }
  },
  {
    path: '/*',
    name: 'not',
    component: resolve => require(['../Layout/index.vue'], resolve),
    redirect: '/notFound',
    meta: {
      isHidden: true,
      isfixed: false,
      title: '404 Not Found'
    },
    children: [
      {
        path: '/notFound',
        name: 'notFound',
        component: resolve => require(['../views/NotFound/NotFound.vue'], resolve),
        meta: {
          isHidden: true,
          isfixed: false,
          title: '404 Not Found'
        }
      },
      {
        path: '/error',
        name: 'error',
        component: resolve => require(['../views/NotFound/ErrorPage.vue'], resolve),
        meta: {
          isHidden: true,
          isfixed: false,
          title: 'Net Error'
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.name === 'login') {
    document.title = '用户登录-大事件'
    next()
  } else {
    if (store.state.user.token) {
      document.title = to.meta.title ? to.meta.title + '-大事件' : '大事件'
      next()
    } else {
      document.title = '用户登录-大事件'
      next('/login')
    }
  }
})
export default router
